<template>
  <div>
    <el-table :data="tableData" height="500" border style="width: 100%" @row-click="onClick">
      <el-table-column prop="name" label="name" />
      <el-table-column prop="gender" label="gender" />
      <el-table-column prop="email" label="email" />
      <el-table-column prop="phone" label="phone" />
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
import { getListData, getPageData } from '@/api/test'
// import axios from 'axios'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      currentIndex: 1,
      pageSize: 10
    }
  },
  mounted() {
    this.getList(100)
    this.getPageList()
  },
  methods: {
    onClick(row, event) {
      console.log(row)
      this.$router.push({
        path: '/mytest/detail',
        query: {
          row
        }
      })
    },
    getList(data) {
      getListData(data).then(res => {
        this.total = res.data.results.length
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getPageList()
    },
    handleCurrentChange(val) {
      this.currentIndex = val
      this.getPageList()
    },
    getPageList() {
      getPageData({
        page: this.currentIndex,
        results: this.pageSize
      }).then(res => {
        console.log(res)
        this.tableData = res.data.results.map(item => {
          return {
            name: item.name.first + '-' + item.name.title + '-' + item.name.last,
            gender: item.gender,
            email: item.email,
            phone: item.phone,
            registered: item.registered.date,
            picture: item.picture.large
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
